//package cn.com.greatwall.system.modules.security.security;
//
//import java.io.IOException;
//import java.util.Objects;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.GenericFilterBean;
//
//import cn.com.greatwall.system.modules.security.config.bean.SecurityProperties;
//import cn.com.greatwall.system.modules.security.service.OnlineUserService;
//import cn.com.greatwall.system.modules.security.service.UserCacheClean;
//import cn.com.greatwall.system.modules.security.service.dto.OnlineUserDto;
//import cn.hutool.core.util.StrUtil;
//import io.jsonwebtoken.ExpiredJwtException;
//
///**
// * @Author herw
// * @Time 2020-06-21 9:53:29 PM
// * @Version 1.0
// * @Description:
// */
//public class TokenFilter extends GenericFilterBean {
//    private static final Logger log = LoggerFactory.getLogger(TokenFilter.class);
//
//    private final TokenProvider tokenProvider;
//    private final SecurityProperties properties;
//    private final OnlineUserService onlineUserService;
//    private final UserCacheClean userCacheClean;
//
//    /**
//     * @param tokenProvider     Token
//     * @param properties        JWT
//     * @param onlineUserService 用户在线
//     * @param userCacheClean    用户缓存清理工具
//     */
//    public TokenFilter(TokenProvider tokenProvider, SecurityProperties properties, OnlineUserService onlineUserService,
//            UserCacheClean userCacheClean) {
//        this.properties = properties;
//        this.onlineUserService = onlineUserService;
//        this.tokenProvider = tokenProvider;
//        this.userCacheClean = userCacheClean;
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
//            throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        String token = resolveToken(httpServletRequest);
//        // 对于 Token 为空的不需要去查 Redis
//        if (StrUtil.isNotBlank(token)) {
//            OnlineUserDto onlineUserDto = null;
//            boolean cleanUserCache = false;
//            try {
//                onlineUserDto = onlineUserService.getOne(properties.getOnlineKey() + token);
//            }
//            catch (ExpiredJwtException e) {
//                log.error(e.getMessage());
//                cleanUserCache = true;
//            } finally {
//                if (cleanUserCache || Objects.isNull(onlineUserDto)) {
//                    userCacheClean.cleanUserCache(
//                            String.valueOf(tokenProvider.getClaims(token).get(TokenProvider.AUTHORITIES_KEY)));
//                }
//            }
//            if (onlineUserDto != null && StringUtils.hasText(token)) {
//                Authentication authentication = tokenProvider.getAuthentication(token);
//                SecurityContextHolder.getContext().setAuthentication(authentication);
//                // Token 续期
//                tokenProvider.checkRenewal(token);
//            }
//        }
//        filterChain.doFilter(servletRequest, servletResponse);
//    }
//
//    /**
//     * 初步检测Token
//     *
//     * @param request /
//     * @return /
//     */
//    private String resolveToken(HttpServletRequest request) {
//        String bearerToken = request.getHeader(properties.getHeader());
//        if (StringUtils.hasText(bearerToken) && bearerToken.startsWith(properties.getTokenStartWith())) {
//            // 去掉令牌前缀
//            return bearerToken.replace(properties.getTokenStartWith(), "");
//        } else {
//            log.debug("非法Token：{}", bearerToken);
//        }
//        return null;
//    }
//}